package com.iseastar.guojiang.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

import com.shouhuobao.station.R;

/**
 * {@link LinearLayoutWithLine}
 * 
 * @author coffee<br>
 *         2016-12-3上午11:44:42
 */
public class RelativeLayoutWithLine extends RelativeLayout {

	private Paint mLinePaint;
	private int paddlingLineLeft;
	private int paddlingLineTop;
	private int paddlingLineRight;
	private int paddlingLineBottom;

	public RelativeLayoutWithLine(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public RelativeLayoutWithLine(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		if (isInEditMode()) {
			return;
		}
		mLinePaint = new Paint();
		// 下划线的颜色
		int linecolor = getResources().getColor(R.color.app_line_color);
		// 如果是8位的带透明度的色值,
		mLinePaint.setColor(linecolor);
		// 单位px
		int colorWidth = (int) getResources().getDimension(R.dimen.app_line_height);
		mLinePaint.setStrokeWidth(colorWidth);
		TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.LayoutWithLine);
		paddlingLineLeft = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineLeft, 0);
		paddlingLineTop = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineTop, 0);
		paddlingLineRight = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineRight, 0);
		paddlingLineBottom = (int) ta.getDimension(R.styleable.LayoutWithLine_paddingLineBottom, 0);
		ta.recycle();
		// 设置false, 否则不执行 #onDraw
		setWillNotDraw(false);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		int top = paddlingLineTop;
		int bottom = getHeight() - paddlingLineBottom;
		int left = paddlingLineLeft;
		int right = getWidth() - paddlingLineRight;
		if (paddlingLineBottom > 0) { // 画底部的线
			canvas.drawLine(left, bottom, right, bottom, mLinePaint);
		}
		if (paddlingLineTop > 0) {
			canvas.drawLine(left, top, right, top, mLinePaint);
		}
	}

	public void setLinePadding(int left, int top, int right, int bottom) {
		this.paddlingLineLeft = left;
		this.paddlingLineTop = top;
		this.paddlingLineRight = right;
		this.paddlingLineBottom = bottom;
		this.invalidate();
	}
}
